//
//  XS_BaseBottomAlertController.swift
//  
//
//  Created by 韩云智 on 2021/10/5.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit

open class XS_BaseBottomAlertController: XS_BaseViewController {
    open lazy var shouldClose = false
    open lazy var actionPS = PublishSubject<(XS_BaseBottomAlertController, XS_AlertActionType)>()
    open lazy var direction: UIRectEdge = UIRectEdge.bottom
    open lazy var contentSize: CGFloat = 400
    open var constraint: Constraint?
    open lazy var contentView: UIView = { [weak self] in
        let contentView = UIView()
        if let `self` = self {
            self.view.addSubview(contentView)
            contentView.snp.makeConstraints {
                switch self.direction {
                case .top:
                    $0.height.equalTo(self.contentSize)
                    $0.left.right.equalToSuperview()
                    self.constraint = $0.top.equalToSuperview().offset(-self.contentSize).constraint
                case .bottom:
                    $0.height.equalTo(self.contentSize)
                    $0.left.right.equalToSuperview()
                    self.constraint = $0.bottom.equalToSuperview().offset(self.contentSize).constraint
                case .left:
                    $0.width.equalTo(self.contentSize)
                    $0.top.bottom.equalToSuperview()
                    self.constraint = $0.left.equalToSuperview().offset(-self.contentSize).constraint
                case .right:
                    $0.width.equalTo(self.contentSize)
                    $0.top.bottom.equalToSuperview()
                    self.constraint = $0.right.equalToSuperview().offset(self.contentSize).constraint
                default: break
                }
            }
        }
        return contentView
    }()
    
    open override func upView() {
        view.alpha = 0
        view.backgroundColor = UIColor(white: 0, alpha: 0.35)
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if view.alpha == 0 {
            view.alpha = 1
            open()
        }
    }
    
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard shouldClose, touches.first(where: { [weak self] in $0.view === self?.contentView }) == nil else { return }
        actionPS.onNext((self, XS_AlertActionType.cancel))
    }
    
    /// 打开动画
    open func open() {
        UIView.animate(withDuration: 0.25) {
            self.constraint?.update(offset: 0)
            self.view.layoutIfNeeded()
            self.contentView.superview?.layoutSubviews()
        }
    }
    /// 收起动画
    open func close(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.25, animations: {
            switch self.direction {
            case .top, .left:
                self.constraint?.update(offset: -self.contentSize)
            case .bottom, .right:
                self.constraint?.update(offset: self.contentSize)
            default: break
            }
            self.view.layoutIfNeeded()
            self.contentView.superview?.layoutSubviews()
            self.view.alpha = 0
        }) { _ in
            self.dismiss(animated: false, completion: completion)
        }
    }
}
